//
//  ContentNaviController.m
//  NOAHWM
//
//  Created by Ryan on 13-5-28.
//  Copyright (c) 2013年 Ryan. All rights reserved.
//

#import "ContentNaviController.h"
#import "AuthViewController.h"
#import "PannelViewController.h"
#import "PersonalInfoViewController.h"
#import "SettingViewController.h"
#import "OnlineProductsViewController.h"
#import "BaseMenuViewController.h"
#import "MyKeepViewController.h"
#import "MyReminderViewController.h"
#import "AccoutProductsViewController.h"
#import "YuYueListViewController.h"
#import "NYWebViewController.h"

#import <QuartzCore/QuartzCore.h>

//#import "BaseDetailViewController.h"

@interface ContentNaviController ()

@property (nonatomic, assign) PannelViewController *pannelViewController;

@end

@implementation ContentNaviController

@synthesize homeViewController;
@synthesize pannelViewController;

- (PannelViewController *)pannelViewController
{
    return (PannelViewController *)[self.view.superview nextResponder];
}

- (HomeViewController *)homeViewController
{
    if(nil == homeViewController)
    {
        homeViewController = [[HomeViewController alloc] init];
    }
    return homeViewController;
}

#pragma mark - Notification methods
- (void)showApplyAuthViewWithNotification:(NSNotification *)notification
{
    [self popToRootViewControllerAnimated:NO];
    AuthViewController *avc = [[AuthViewController alloc] init];
    avc.isMenu = YES;
    [self pushViewController:avc animated:NO];
    [avc release];
}

- (void)showPersonalInfoViewWithNotification:(NSNotification *)notification
{
    [self popToRootViewControllerAnimated:NO];
    PersonalInfoViewController *pivc = [[PersonalInfoViewController alloc] init];
    [self pushViewController:pivc animated:NO];
    [pivc release];
}

- (void)showSettingViewWithNotification:(NSNotification *)notification
{
    [self popToRootViewControllerAnimated:NO];
    SettingViewController *svc = [[SettingViewController alloc] init];
    [self pushViewController:svc animated:NO];
    [svc release];
}

- (void)showOnlineProsViewWithNotification:(NSNotification *)notification
{
    [self popToRootViewControllerAnimated:NO];
    NSDictionary *dict = notification.userInfo;
    
    OnlineProductsViewController *opvc = [[OnlineProductsViewController alloc] init];
    opvc.productTypeId = [dict objectForKey:@"productName"];
    opvc.menuName = [dict objectForKey:@"menuName"];
    [self pushViewController:opvc animated:NO];
    [opvc release];
}

- (void)showAccountProsViewWithNotification:(NSNotification *)notification
{
    [self popToRootViewControllerAnimated:NO];
    NSDictionary *dict = notification.userInfo;
    AccoutProductsViewController *apvc = [[AccoutProductsViewController alloc] init];
    apvc.productTypeId = [dict objectForKey:@"productName"];
    [self pushViewController:apvc animated:NO];
    [apvc release];
}

- (void)showMyKeepViewWithcNotification:(NSNotification *)notification
{
    [self popToRootViewControllerAnimated:NO];
    MyKeepViewController *mkvc = [[MyKeepViewController alloc]init];
    [self pushViewController:mkvc animated:NO];
    [mkvc release];
}

- (void)showMyReminderWithNotification:(NSNotification *)notification
{
    [self popToRootViewControllerAnimated:NO];
    MyReminderViewController *mrvc = [[MyReminderViewController alloc]init];
    [self pushViewController:mrvc animated:NO];
    [mrvc release];
}

- (void)showYuyueListViewWithNotification:(NSNotification *)notification
{
    [self popToRootViewControllerAnimated:NO];
    YuYueListViewController *yylvc = [[YuYueListViewController alloc] init];
    yylvc.isFromRightMenu = YES;
    yylvc.yuyueEnter = kRightMenuEnter;
    [self pushViewController:yylvc animated:NO];
    [yylvc release];
    
}

- (void)showUserGradeViewWithNotification:(NSNotification *)notification
{
    [self popToRootViewControllerAnimated:NO];
    NYWebViewController *nywvc = [[NYWebViewController alloc] init];
    nywvc.htmlType = kAboutGradeHtml;
    [self pushViewController:nywvc animated:NO];
    [nywvc release];
}
#pragma mark- UINavigationController methods

- (id)initWithRootViewController:(UIViewController *)rootViewController
{
    if(self = [super initWithRootViewController:self.homeViewController])
    {}
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showApplyAuthViewWithNotification:) name:kShowApplyAuthViewNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showPersonalInfoViewWithNotification:) name:kShowPersonalInfoViewNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showSettingViewWithNotification:) name:kShowSettingViewNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showOnlineProsViewWithNotification:) name:kShowOnlineProsViewNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showAccountProsViewWithNotification:) name:kShowAccountProsViewNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showMyKeepViewWithcNotification:) name:kShowMyKeepNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showMyReminderWithNotification:) name:kShowMyReminderNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showYuyueListViewWithNotification:) name:kShowYuyueListViewNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showUserGradeViewWithNotification:) name:kShowUserGradeViewNotification object:nil];
    
    self.navigationBar.hidden = YES;
    self.view.layer.shadowOffset = CGSizeZero;
    self.view.layer.shadowOpacity = 0.75f;
    self.view.layer.shadowRadius = 5.f;
    self.view.layer.shadowColor = [UIColor blackColor].CGColor;
    self.view.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.view.layer.bounds].CGPath;
    self.view.clipsToBounds = NO;
    self.delegate = self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.isViewLoaded && !self.view.window)
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        self.view = nil;
        self.homeViewController = nil;
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    self.homeViewController = nil;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [homeViewController release];
    [super dealloc];
}

#pragma mark - UINavigationControllerDelegate methods
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if([viewController isKindOfClass:[BaseMenuViewController class]])
    {
        //认证页有两种方式 1:个人中心-申请认证(menu) 2:在线产品-产品详情-认证(not menu)
        if([viewController isKindOfClass:[AuthViewController class]])
        {
            AuthViewController *avc = (AuthViewController *)viewController;
            if(avc.isMenu)
                [[NSNotificationCenter defaultCenter] postNotificationName:kActiveNaviPanGestureNotification object:nil];
            else
                [[NSNotificationCenter defaultCenter] postNotificationName:kActiveNaviSwipeGestureNotification object:nil];
        }
        else if([viewController isKindOfClass:[YuYueListViewController class]])
        {
            YuYueListViewController *yylvc = (YuYueListViewController *)viewController;
            if(yylvc.isFromRightMenu)
                [[NSNotificationCenter defaultCenter] postNotificationName:kActiveNaviPanGestureNotification object:nil];
            else
                [[NSNotificationCenter defaultCenter] postNotificationName:kActiveNaviSwipeGestureNotification object:nil];
        }
        else if([viewController isKindOfClass:[NYWebViewController class]])
        {
            NYWebViewController *nywvc = (NYWebViewController *)viewController;
            if(nywvc.htmlType == kAboutGradeHtml)
                [[NSNotificationCenter defaultCenter] postNotificationName:kActiveNaviPanGestureNotification object:nil];
            else
                [[NSNotificationCenter defaultCenter] postNotificationName:kActiveNaviSwipeGestureNotification object:nil];
        }
        else
            [[NSNotificationCenter defaultCenter] postNotificationName:kActiveNaviPanGestureNotification object:nil];
    }
    else
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:kActiveNaviSwipeGestureNotification object:nil];
    }
}
@end
